package com.kh.first;

import com.kh.first.plugin.MybatisLog;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.session.Configuration;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.MultipartConfigElement;
import java.io.File;

@SpringBootApplication
 @MapperScan(basePackages="com.kh.first.dao"  )//官方建议，同级
public class MyprojectApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyprojectApplication.class, args);
    }

    /**
     * 设置匹配*.action后缀请求
     *
     * @param dispatcherServlet
     * @return
     */
    @Bean
    public ServletRegistrationBean servletRegistrationBean(DispatcherServlet dispatcherServlet) {
        ServletRegistrationBean servletServletRegistrationBean = new ServletRegistrationBean(dispatcherServlet);
        servletServletRegistrationBean.addUrlMappings("*.do");
        servletServletRegistrationBean.addUrlMappings("*.html");
        servletServletRegistrationBean.addUrlMappings("*.css");
        servletServletRegistrationBean.addUrlMappings("*.js");
        servletServletRegistrationBean.addUrlMappings("*.ttf");
        servletServletRegistrationBean.addUrlMappings("*.TTF");
        servletServletRegistrationBean.addUrlMappings("*.woff");
        servletServletRegistrationBean.addUrlMappings("*.woff2");
        servletServletRegistrationBean.addUrlMappings("*.png");
        servletServletRegistrationBean.addUrlMappings("*.gif");
        servletServletRegistrationBean.addUrlMappings("*.ico");
        servletServletRegistrationBean.addUrlMappings("*.jpeg");
        servletServletRegistrationBean.addUrlMappings("*.jpg");
        servletServletRegistrationBean.addUrlMappings("*.json");
        servletServletRegistrationBean.addUrlMappings("*.txt");
        return servletServletRegistrationBean;
    }

    @Bean
    public MultipartResolver resolver() {
        return new CommonsMultipartResolver();
    }
    @Value("${upload.image.path}")
    String imagePath;
    @Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //判断是否有文件夹，没有就创建
        File file = new File(imagePath);
        if (!file.exists()) {
            file.getParentFile().mkdirs();
        }
        //文件路径，如：d:\temp\
        factory.setLocation(imagePath);
        return factory.createMultipartConfig();
    }

    @Bean
    public ConfigurationCustomizer configurationMybatis() {
		return new ConfigurationCustomizer() {
            @Override
            public void customize(Configuration configuration) {
                configuration.addInterceptor(new MybatisLog());
            }
        };
    }
}
